package com.yxkj.syh.app.huarong.activities.creat;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.bean.LiveDataEvent;
import com.syh.app.basic.config.BasicConstants;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.activities.creat.add_goods.AddGoodsActivity;
import com.yxkj.syh.app.huarong.activities.creat.address.edit.EditAddressActivity;
import com.yxkj.syh.app.huarong.activities.creat.bank.BankCardsActivity;
import com.yxkj.syh.app.huarong.activities.creat.bank.edit.EditBankCardsActivity;
import com.yxkj.syh.app.huarong.activities.item_select.ItemSelectActivity;
import com.yxkj.syh.app.huarong.activities.links.save.EditLinkActivity;
import com.yxkj.syh.app.huarong.adps.AddedGoodsAdp;
import com.yxkj.syh.app.huarong.bean.Buyer;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.bean.Linkman;
import com.yxkj.syh.app.huarong.bean.PaperFactory;
import com.yxkj.syh.app.huarong.bean.Payment;
import com.yxkj.syh.app.huarong.bean.UserMainInfo;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityNewOrderBinding;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * 创建订单
 */
@Route(path = ArouterPath.NEW_ORDER_ACTIVITY)
public class NewOrderActivity extends BaseActivity<ActivityNewOrderBinding, NewOrderVM> {
    AddedGoodsAdp mAddedGoodsAdp;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_new_order;
    }

    @Override
    protected int getViewModelId() {
        return BR.newOrderVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mVDBinding.titleView.setLeftOnClickListener(v -> {
            finish();
        });

        mVDBinding.llSupplier.setOnClickListener(v -> {
            Intent intent = new Intent(NewOrderActivity.this, ItemSelectActivity.class);
            intent.putExtra("type", ItemSelectActivity.SUPPLIER);
            startActivityForResult(intent, 0x1a);

        });
        mVDBinding.llFactory.setOnClickListener(v -> {
            Intent intent = new Intent(NewOrderActivity.this, ItemSelectActivity.class);
            intent.putExtra("type", ItemSelectActivity.FACTORY);
            startActivityForResult(intent, 0x2a);
        });
        mVDBinding.llPayment.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mViewModel.ofPayment.get())) {
                if (mViewModel.getLinkman() != null) {
                    Intent intent = new Intent(NewOrderActivity.this, EditBankCardsActivity.class);
                    intent.putExtra("linkmanID", mViewModel.getLinkman().getId());
                    startActivity(intent);
                }
            } else {
                Intent intent = new Intent(NewOrderActivity.this, BankCardsActivity.class);
                intent.putExtra("isSelect", true);
                intent.putExtra("id", mViewModel.getLinkman().getId());
                startActivityForResult(intent, 0x3a);
            }
        });
        mVDBinding.llFromAddress.setOnClickListener(v -> {
            if (mViewModel.getLinkman()!=null){
                Intent intent = new Intent(NewOrderActivity.this, EditLinkActivity.class);
                intent.putExtra("id", mViewModel.getLinkman().getId());
                intent.putExtra("isOrderRequested", true);
                startActivityForResult(intent, 0x5a);
            }
        });
        mVDBinding.tvAddGoods.setOnClickListener(v -> {
            addGoods(null);
        });

        mAddedGoodsAdp = new AddedGoodsAdp();
        mAddedGoodsAdp.setOnClick(goods -> {
            addGoods(goods);
        });
        mAddedGoodsAdp.setIsCreator(true);
        mVDBinding.rvGoods.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mVDBinding.rvGoods.setAdapter(mAddedGoodsAdp);
        mViewModel.setAddedGoodsAdp(mAddedGoodsAdp);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldCreatedSuccess.observe(this, orderInfo -> {
            new AlertDialog.Builder(NewOrderActivity.this).setMessage("是否立即发货？").setCancelable(false)
                    .setNegativeButton("暂不发货", (dialog, which) -> {
                        EventBus.getDefault().post(new LiveDataEvent(BasicConstants.EVENT_SELECT_ORDER_ALL));
                        dialog.cancel();
                        finish();
                    }).setPositiveButton("立即发货", (dialog, which) -> {
                dialog.cancel();
                EventBus.getDefault().post(new LiveDataEvent(BasicConstants.EVENT_SELECT_ORDER_ALL));
                ARouter.getInstance().build(ArouterPath.EDIT_ORDER_ACTIVITY).withSerializable("orderInfo", orderInfo).navigation();
                finish();
            }).create().show();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(mViewModel.ofPayment.get()) && mViewModel.getLinkman() != null) {
            mViewModel.getPaymentList();
        }
    }

    private void addGoods(Goods clickedGoods) {

        ArrayList<String> added = new ArrayList<>();
        for (Goods goods : mAddedGoodsAdp.getData()) {
            String tag = goods.getCode();
            added.add(tag);
        }
        Intent intent = new Intent(NewOrderActivity.this, AddGoodsActivity.class);
        intent.putStringArrayListExtra("added", added);
        if (clickedGoods != null) {
            intent.putExtra("preSelectedGoods", clickedGoods);
        }
        startActivityForResult(intent, 0x4a);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0x1a) {
                mViewModel.setLinkman((Linkman) data.getSerializableExtra("data"));
            } else if (requestCode == 0x2a) {
                mViewModel.setPaperFactory((PaperFactory) data.getSerializableExtra("data"));
            } else if (requestCode == 0x3a) {
                mViewModel.setPayment((Payment) data.getSerializableExtra("data"));
            } else if (requestCode == 0x4a) {
                Goods goods = (Goods) data.getSerializableExtra("goods");
                int operate = data.getIntExtra("operate", -1);
                if (operate == 0) {//添加
                    mAddedGoodsAdp.add(goods);
                } else if (operate == 1) {//移除
                    mAddedGoodsAdp.remove(goods);
                } else if (operate == 2) {//编辑
                    mAddedGoodsAdp.change(goods);
                }
            } else if (requestCode == 0x5a) {
                Linkman supplier = (Linkman) data.getSerializableExtra("data");
                mViewModel.setLinkman(supplier);
            }
        }
    }
}
